<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="开单日期" prop="orderDate" label-width="100px">
        <el-date-picker
          v-model="queryParams.orderDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="生产单号" prop="internalOrderNo">
        <el-input
          v-model="queryParams.internalOrderNo"
          placeholder="请输入生产单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable class="!w-200px">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="款式" prop="internalOrderNo">
        <el-input
          v-model="queryParams.internalOrderNo"
          placeholder="请输入款式"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="客户简称" prop="quotationNo">
        <el-input
          v-model="queryParams.quotationNo"
          placeholder="请输入客户简称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="quotationNo">
        <el-input
          v-model="queryParams.quotationNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="货号" prop="customerOrderNo">
        <el-input
          v-model="queryParams.customerOrderNo"
          placeholder="请输入货号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="品名" prop="customerOrderNo">
        <el-input
          v-model="queryParams.customerOrderNo"
          placeholder="请输入品名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="规格" prop="customerOrderNo">
        <el-input
          v-model="queryParams.customerOrderNo"
          placeholder="请输入规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="客户编号" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择客户编号" clearable class="!w-200px">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择客户" clearable class="!w-200px">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="财务审核" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择财务审核" clearable class="!w-200px">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="只看自己单" prop="status">
        <el-checkbox v-model="queryParams.status" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('edit')"
          v-hasPermi="['erp:sale-product-order:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <ReusableTable
      :loading="loading"
      :tableData="list"
      :mergeFields="['internalOrderNo']"
      :columns="[
        ...columns,
        {
          label: '操作',
          prop: 'action',
          align: 'center',
          slot: 'action',
          width: 200,
          fixed: 'right',
          buttons: [
            { text: '编辑', action: 'edit', type: 'primary', link: true },
            { text: '删除', action: 'delete', type: 'danger', link: true }
          ]
        }
      ]"
      :is-disabled="true"
      @button-click="handleTableButton"
      @current-change="handleCurrentChange"
    >
      <template #action="{ row }">
        <el-button
          type="text"
          size="small"
          @click="openForm('edit', row.id)"
          v-hasPermi="['erp:sale-product-order:edit']"
        >编辑</el-button>
        <el-button
          type="text"
          size="small"
          @click="handleDelete(row.id)"
          v-hasPermi="['erp:sale-product-order:delete']"
        >删除</el-button>
      </template>
    </ReusableTable>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { SaleProductOrderApi, SaleProductOrderVO } from '@/api/erp/saleproductorder'
import { columns } from './index.d'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { push } = useRouter()

const loading = ref(true) // 列表的加载中
const list = ref<SaleProductOrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  internalOrderNo: undefined,
  customerName: undefined,
  orderDate: [],
  deliveryDate: [],
  quotationNo: undefined,
  customerOrderNo: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SaleProductOrderApi.getSaleProductOrderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  push({ path: 'boxproducterdetail', state: { type: type, id: id } })
  // push({ name: 'boxproducterdetail', params: { type: type, id: id } })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SaleProductOrderApi.deleteSaleProductOrder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 选中行操作 */
const currentRow = ref({}) // 选中行
const handleCurrentChange = (row) => {
  currentRow.value = row
}

const handleTableButton = ({ action, row }) => {
  switch (action) {
    case 'edit':
      openForm('edit', row.id)
      break
    case 'view':
      openForm('view', row.id)
      break
    case 'delete':
      handleDelete(row.id)
      break
    default:
      console.warn(`未处理的操作: ${action}`)
  }
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
